package com.doudou.oauth.validate.sms;

import com.doudou.oauth.constant.SysConstant;
import com.doudou.oauth.validate.ValidateCode;
import com.doudou.oauth.validate.ValidateCodeGenerator;
import com.doudou.oauth.validate.properties.CustomSecurityProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * @Author: 傻男人
 * @Date: 2020/5/21 15:27
 * @Version: 1.0
 * @Description: 短信验证码生成器
 */
@Slf4j
@Component("smsValidateCodeGenerator")
public class SmsCodeGenerator implements ValidateCodeGenerator {

    @Autowired
    private CustomSecurityProperties customSecurityProperties;

    /**
     * 生成验证码
     *
     * @param request
     * @return
     */
    @Override
    public ValidateCode generate(ServletWebRequest request) {
        String code = RandomStringUtils.randomNumeric(customSecurityProperties.getCode().getSms().getLength());
        log.info("--------------------生成的短信验证码为："+code+"----------------");
        return new ValidateCode(code, ServletRequestUtils.getIntParameter(request.getRequest(), SysConstant.EXPIRE_IN, customSecurityProperties.getCode().getImage().getExpireIn()));
    }
}